<template>
  <div class="article">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>

    <!--loading-->
    <div class="loading" v-if="loading">
      <van-loading class="inner" type="spinner" color="#1989fa" />
    </div>

    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">作者: {{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | fromNow }}</p>
        </div>
        <van-button round size="small" type="info" @click="followFn">{{
          article.is_followed ? '取消关注' : '+ 关注'
        }}</van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
        <!-- <div>正文内容稍后回来。。。</div> -->
      </div>

      <!--分割线-->
      <van-divider>END</van-divider>

      <!--按钮-->
      <div class="btns">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          icon="good-job-o"
          @click="zanFn"
        >
          {{ article.attitude === 1 ? '取消点赞' : '点赞' }}
        </van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          plain
          icon="delete-o"
        >
          {{ article.attitude === 0 ? '取消不喜欢' : '不喜欢' }}
        </van-button>
      </div>
    </div>

    <!-- 评论组件 -->
    <common></common>
  </div>
</template>

<script>
import {
  getArticleInfo,
  addFollowUser,
  unFollowUser,
  deleteLike,
  addLike
} from '@/api/article'
import { mapState } from 'vuex'

import common from './common.vue'

export default {
  name: 'Article',
  data() {
    return {
      loading: true,
      article: {}
    }
  },
  components: {
    common
  },
  computed: {
    ...mapState('user', ['token']),
    artId() {
      // 计算文章的id ==> 当前路由对象的 params 获取到
      // console.log(this.$route)
      return this.$route.params.id
    }
  },
  async created() {
    const res = await getArticleInfo(this.artId)
    console.log(res)

    this.article = res.data
    // 隐藏loading效果
    this.loading = false
  },
  methods: {
    async zanFn() {
      // 点赞操作
      if (this.token.token) {
        // 已经登录
        console.log('已经登录')

        // 点赞  or  取消点赞

        if (this.article.attitude === 1) {
          // 取消点赞
          await deleteLike(this.article.art_id)
          this.article.attitude = -1
        } else {
          // 点赞
          await addLike(this.article.art_id)
          this.article.attitude = 1
        }

        this.$toast('操作成功')
      } else {
        // 未登录
        console.log('未登录')
        // 跳转login页面  ==> 登录之后跳转回来的是首页（之后来修复问题）
        this.$router.push('/login')
      }
    },
    async followFn() {
      // 步骤
      //  1. 判断用户是否有登录过 ==> vuex 的 token  ==> this.$store.state.user.token.token ==> 辅助函数 mapState
      //  1.1 如果没有登录，跳转login页面
      //  1.2 如果已登录
      //     区分   关注  or   取关

      if (this.token.token) {
        // 已经登录
        console.log('已经登录')

        // 发送请求 (关注  or   取关)
        if (this.article.is_followed) {
          // 发送取消关注的请求
          console.log('发送取消关注的请求')
          await unFollowUser(this.article.aut_id)

          // 请求之后，来个提示
          this.$toast('取消关注成功')
        } else {
          // 发送关注的请求
          console.log('发送关注的请求')
          await addFollowUser(this.article.aut_id)

          this.$toast('关注成功')
        }

        // 请求之后，将is_followed结果置反
        this.article.is_followed = !this.article.is_followed
      } else {
        // 未登录
        console.log('未登录')
        // 跳转login页面  ==> 登录之后跳转回来的是首页（之后来修复问题）
        // this.$router.push('/login')

        // 不仅仅是要跳转到/login ，还要带上回来的路
        // console.log(this.$route.fullPath) // /article/6471

        this.$router.push({
          path: '/login',
          query: {
            // flag标识，有了标识，将来不要使用push，登录回退使用路由的back方法进行回退
            flag: true,
            // 携带查询参数
            back: this.$route.fullPath
          }
        })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.loading {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
  .btns {
    width: 50%;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    align-content: center;
  }
}
</style>
